package com.bjc.platform.security;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;












import com.bjc.platform.model.AdmLog;
import com.bjc.platform.model.AdmUser;
import com.bjc.platform.model.CustomUserDetails;
import com.bjc.platform.repository.AdmLogRepo;
import com.bjc.platform.repository.AdmUserRepo;
import com.bjc.platform.util.IpUtil;
import com.bjc.platform.util.StaticMethod;

/**
 * handler for authentication success
 * 
 * @author BJC
 */
public class AuthenticationSuccessHandler extends
		SavedRequestAwareAuthenticationSuccessHandler {

	@Autowired
	private AdmLogRepo admLogRepo;
	
	@Autowired
	private AdmUserRepo admUserRepo;

	/**
	 * 自定义认证成功处理
	 */
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws ServletException, IOException {
		CustomUserDetails user = StaticMethod.getUser();
		AdmLog admLog = new AdmLog();
		admLog.setSource(IpUtil.getIpAddr(request));
		admLog.setType("INFO");
		admLog.setUrl(request.getRequestURI());
		admLog.setFuncName("login");
		admLog.setDescription("登录成功");
		admLog.setLogTime(new Date());
		admLog.setUserId(user.getId());
		admLogRepo.save(admLog);

		super.onAuthenticationSuccess(request, response, authentication);
	}
}
